package com.ag.dgm.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.dao.DataAccessException;

import com.ag.bean.TbAdviceType;
import com.ag.bean.TbDisposalAdvice;
import com.ag.bean.TbEvent;
import com.ag.bean.TbEventPlan;
import com.ag.bean.TbEventPlanStart;
import com.ag.bean.TbPlan;
import com.ag.bean.TbPlanStandard;

/**
 * 
 * @author dgm
 * 查询事件的处置建议
 *
 */
public class TbDisposalAdviceDAO extends com.ag.dao.TbDisposalAdviceDAO {
	private static final Log log = LogFactory.getLog(TbDisposalAdviceDAO.class);

	/*
	 * 查全部的disposalAdvice
	 */
	public List<TbDisposalAdvice> findAdviceByAllType(String Eid) {
		System.out.println("方法findAdviceByAllType_dao");
		String hql = null;
		hql = "from TbDisposalAdvice t  left  join fetch t.tbUserByAdviceUId  left  join fetch t.tbUserBySubmitUId  left join fetch t.tbUserByApplyUId where t.tbEvent.EId = "+Eid+"  and t.c=1003 order by t.daSubmitTime desc";
		Session session=null;
		Transaction transaction=null;
		List<TbDisposalAdvice> list = null;
		try {
			 session = (Session)this.getSessionFactory().openSession();
			transaction = session.beginTransaction();
			Query query = (Query) session.createQuery(hql);
			list = query.list();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			log.error("dgm.dao.TbDisposalAdviceDAO.findAdviceByAllTypes() Exception rollback Transaction Session closed!");
		} finally {
			/*if (transaction != null) {
				transaction.rollback();
			}*/
			if (session != null) {
				session.close();
			}
		}

		return list;
	}
	
	/*
	 * 通过Type查disposalAdvice
	 */
	public List<TbDisposalAdvice> findAdviceByType(String atId,String Eid) {
		System.out.println("这是findAdviceByType方法");
		String hql = "from TbDisposalAdvice t  left  join fetch t.tbUserByAdviceUId  left  join fetch t.tbUserBySubmitUId  left join fetch t.tbUserByApplyUId   where t.tbAdviceType.atId ="+atId+" and t.tbEvent.EId ="+Eid+" and t.c=1003  order by t.daSubmitTime desc";
		
		Session session=null;
		Transaction transaction=null;
		List<TbDisposalAdvice> list = null;
		try {
			 session = (Session)this.getSessionFactory().openSession();
			transaction = session.beginTransaction();
			Query query = (Query) session.createQuery(hql);
			list = query.list();
			for(TbDisposalAdvice t:list){
				System.out.println(t.getDaContent());
			}
			session.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			log.error("dgm.dao.TbDisposalAdviceDAO.findAdviceByAllTypes() Exception rollback Transaction Session closed!");
		} finally {
			/*if (transaction != null) {
				transaction.rollback();
			}*/
			if (session != null) {
				session.close();
			}
			
		}
		return list;
	}

	public TbPlan queryEventPlan(String eid) {
		System.out.println("这是queryEventPlan_dao方法");
//		String hql="select t.tbPlan.PId,t.tbPlan.PName from TbEventPlan t where t.tbEvent.EId="+eid;
		String hql="from TbPlan t where t.PId=(select p.tbPlan.PId from TbEventPlan p where p.tbEvent.EId="+eid+")";
		TbPlan tbPlan = null;
		try {
			List<TbPlan> tList= this.getHibernateTemplate().find(hql);
			if(tList!=null&&tList.size()!=0)
				tbPlan=tList.get(0);
//			System.out.println(tList.size()+"^^^^^^^^^");
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("^^^^"+tbPlan);
		//以上查到一个包含planid和planName 的list
		return tbPlan;
	}


	public void updateEventLevel(String eventLevel, String eid) {
		System.out.println("这是updateEventLevel_dao");
		String hql="update TbEvent t set t.tbEventLevel.elId="+eventLevel+" where t.EId="+eid;
		Session session=null;
		try {
			 session = (Session)this.getSessionFactory().openSession();
			Transaction transaction = session.beginTransaction();
			session.createQuery(hql).executeUpdate();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			log.error("dgm.dao.TbDisposalAdviceDAO.findAdviceByAllTypes() Exception rollback Transaction Session closed!");
		} finally {
			/*if (transaction != null) {
				transaction.rollback();
			}*/
			if (session != null) {
				session.close();
			}
		}
	}

	public List<TbPlanStandard> queryEventPlanStandard(String eid) {
		System.out.println("这是queryEventPlanStandard_dao方法");
		String hql="from TbPlanStandard t where t.a=0 and t.elId " +
				"in (select ep.id.elId from TbEventPlanStart ep " +
				"where ep.tbEvent.EId="+eid+")";
		return this.getHibernateTemplate().find(hql);
	}

	
	public String queryPlanReason(String eid) {
		System.out.println("这是queryPlanReason_dao方法");
		String hql="from TbEvent t where t.EId="+eid;
		TbEvent tbEvent = (TbEvent) this.getHibernateTemplate().find(hql).get(0);
		System.out.println(tbEvent.getPlanReason()+"&&");
		return tbEvent.getPlanReason();
	}
/**
 * 
 * @param eid 事件id
 * 通过事件id，删除预案和预案相关
 */
	public void delPlanByEvent(String eid) {
	System.out.println("这是delPlanByEvent_dao方法");
		//事件的plan_reason设为空
		String hql_updatePlanReason="update TbEvent t set t.planReason = null where t.EId="+eid;
		updateHqlStatement(hql_updatePlanReason);
		//事件启动标准设为空
		String hql_emptyEventPlanStandard="delete TbEventPlanStart t where t.tbEvent.EId="+eid;
		updateHqlStatement(hql_emptyEventPlanStandard);
		//事件的预案设为空
		String hql_emptyEventPlan="delete TbEventPlan t where t.tbEvent.EId="+eid;
		updateHqlStatement(hql_emptyEventPlan);
	}
	
	public void updateHqlStatement(String hql){
		System.out.println("这是updateHqlStatement_dao");
		Session session=null;
		try {
			 session = (Session)this.getSessionFactory().openSession();
			Transaction transaction = session.beginTransaction();
			session.createQuery(hql).executeUpdate();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			/*if (transaction != null) {
				transaction.rollback();
			}*/
			if (session != null) {
				session.close();
			}
		}
	}

}
